﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ImagesViewer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            win1_SizeChanged(null, null);
        }

        private void buttonPrev_Click(object sender, RoutedEventArgs e)
        {
            showPrev();
        }

        private void buttonNext_Click(object sender, RoutedEventArgs e)
        {
            showNext();
        }

        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            deleteCurrent();
        }

        private void Window_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if(this.WindowState != System.Windows.WindowState.Maximized)
            {
                this.WindowState = System.Windows.WindowState.Maximized;
            }
            else
            {
                this.WindowState = System.Windows.WindowState.Normal;
            }
        }

        private void win1_KeyDown(object sender, KeyEventArgs e)
        {
            switch(e.Key)
            {
                case Key.Delete:
                    deleteCurrent();
                    e.Handled = true;
                    break;
                case Key.Up:
                case Key.Left:
                    showPrev();
                    e.Handled = true;
                    break;
                case Key.Down:
                case Key.Right:
                    showNext();
                    e.Handled = true;
                    break;
                case Key.Home:
                    showFirst();
                    e.Handled = true;
                    break;
                case Key.End:
                    showLast();
                    e.Handled = true;
                    break;
                case Key.C:
                    if(Keyboard.Modifiers == ModifierKeys.Control)
                    {
                        if (ExifUtils.Exif.IsGps)
                        {
                            Clipboard.SetText(ExifUtils.Exif.Location);
                        }
                        else
                        {
                            MessageBox.Show("No GPS data!", "EXIF");
                        }
                        e.Handled = true;
                    }
                    break;
            }
        }

        private void Window_DragOver(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Copy;
        }

        private void Window_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                //
                e.Effects = DragDropEffects.Copy;
                //
                //
                string[] droppedFiles = (string[])e.Data.GetData(DataFormats.FileDrop);
                //
                loadFiles(droppedFiles[0]);
                //
                if(0==files.Count())
                {
                    infoClean();
                    image1.Source = null;
                    this.WindowState = System.Windows.WindowState.Normal;
                }
                showFile(files[currentImage]);
                infoShow(files[currentImage]);
                this.WindowState = System.Windows.WindowState.Maximized;
                //
            }
            //
            e.Effects = DragDropEffects.None;
            //
        }

        private void Window_DragEnter(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Copy;
        }




        //====================================================================================================================
        private class ItemData
        {
            public bool isInCenter;
            public int Width;

        }

        private void win1_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            //
            // Element, który ma być wycentrowany ma mieć w Tag wisaną '*'
            // Położenie jest regulowane szerokością zerowego elementu: Label
            //
            List<ItemData> ids = new List<ItemData>();
            //
            int freeSpaceWidth = CClientAreaSize.GetClientWidth(this);
            //int freeSpaceWidth = (int)win1.ActualWidth - (2*(int)SystemParameters.ResizeFrameVerticalBorderWidth);
            int widthOfAllElements = 0;
            //
            for (int k = 0; k < toolStrip.Children.Count; k++)
            {
                var x = toolStrip.Children[k];
                Label l = x as Label;
                Button b = x as Button;
                TextBlock p = x as TextBlock;
                if (null != l)
                {
                    ids.Add(new ItemData() { isInCenter = ("*" == (string)l.Tag), Width = (int)l.Width });
                    widthOfAllElements += (int)l.Width;
                }
                else if (null != b)
                {
                    ids.Add(new ItemData() { isInCenter = ("*" == (string)b.Tag), Width = (int)b.Width });
                    widthOfAllElements += (int)b.Width;
                }
                else if (null != p)
                {
                    ids.Add(new ItemData() { isInCenter = ("*" == (string)p.Tag), Width = (int)p.Width });
                    widthOfAllElements += (int)p.Width;
                }
                else
                {
                    ids.Add(new ItemData() { isInCenter = false, Width = 1 });
                    widthOfAllElements += 1;
                }
            }
            //
            //
            //
            //
            //
            //
            //
            //
            //
            int widthOfCenterElement = (from x in ids where x.isInCenter select x.Width).SingleOrDefault<int>();
            //
            int widthElementsWithoutPad = 0;
            for (int i = 1; i < ids.Count; i++)
            {
                if (ids[i].isInCenter) break;
                widthElementsWithoutPad += ids[i].Width;
            }
            //
            //
            //
            if ((widthElementsWithoutPad + 2) >= freeSpaceWidth)
            {
                (toolStrip.Children[0] as Label).Width = 1;
                return;
            }
            //
            //
            int widhOfZeroElement = ((freeSpaceWidth - 2) / 2) - ((widthElementsWithoutPad + (widthOfCenterElement / 2)));
            //
            if ((widhOfZeroElement + widthElementsWithoutPad) < (freeSpaceWidth - 2))
            {
                (toolStrip.Children[0] as Label).Width = widhOfZeroElement;
            }
            else
            {
                widhOfZeroElement = (freeSpaceWidth - 1) - widthElementsWithoutPad;
                if (widhOfZeroElement < 0) widhOfZeroElement = 1;
                (toolStrip.Children[0] as Label).Width = widhOfZeroElement;
            }

        }


        //====================================================================================================================

    }
}
